
package Controllers;
import Configs.Config;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Predicate;
import com.db4o.query.Query;
import static java.lang.reflect.Array.set;
import java.util.Set;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import models.NhanVien;
import models.TaiKhoan;

/**
 *
 * @author Dinh Nhan
 */
public class TaiKhoanController {
    private  Config conn;
    private  NhanVienController nvc;
    
    public TaiKhoanController(Config conn){
        this.conn = conn;
        this.nvc  = new NhanVienController(conn);
    }
    //lay tat ca tk
    public ObjectSet<TaiKhoan> getAllTaiKhoan(){
           Query query = conn.getConn().query();
           query.constrain(TaiKhoan.class);
           ObjectSet<TaiKhoan> tks = query.execute();
           return tks;
       }
    
    //lay tk bang ten tk
    public TaiKhoan getWithTenTaiKhoan(String tentaikhoan) {
  
        ObjectSet<TaiKhoan> tks = this.conn.getConn().query(new Predicate<TaiKhoan>() {
            @Override
            public boolean match(TaiKhoan taikhoan) {
                return (taikhoan.getTaikhoan().equals(tentaikhoan));
            }
        });
        if(tks.isEmpty())
              return null;
        else
              return tks.get(0);
  
    }
    //lay de tim kiem
    public ObjectSet<TaiKhoan> getWithTenTaiKhoan_Gandung(String tim) { 
       Query query = this.conn.getConn().query();
       query.constrain(TaiKhoan.class);
       query.descend("taikhoan").constrain(tim).like().endsWith(true).startsWith(true);
       query.descend("isDel").constrain(false).equal();
       ObjectSet<TaiKhoan> tks = query.execute();
       return tks;
    }
    //check tk co ton tai chua
    public boolean checkexist(String tkcheck){
        TaiKhoan tk = this.getWithTenTaiKhoan(tkcheck);
        if(tk == null)
               return false;
        else
                return true;
    }
    
    public void showAllTaiKhoan(JTable tbdsnv){
        DefaultTableModel model = (DefaultTableModel) tbdsnv.getModel();
        ObjectSet<TaiKhoan> tks = this.getAllTaiKhoan();
       
        int stt =0;
        if(tks.size() !=0){
            for(TaiKhoan tk : tks){
                if(tk.getIsdelete() == false){
                    Object[]  row = {++stt, tk.getTaikhoan(),nvc.getWithMSNV(tk.getMSNV()).getHoten()};
                    model.addRow(row);
                }
            }
        }
        
    }
    
        public void showAllTaiKhoan_isDelete(JTable tbdsnv){
        DefaultTableModel model = (DefaultTableModel) tbdsnv.getModel();
        ObjectSet<TaiKhoan> tks = this.getAllTaiKhoan();
       
        int stt =0;
        if(tks.size() !=0){
            for(TaiKhoan tk : tks){
                if(tk.getIsdelete() == true){
                    Object[]  row = {++stt, tk.getTaikhoan(),nvc.getWithMSNV(tk.getMSNV()).getHoten()};
                    model.addRow(row);
                }
            }
        }
        
    }
    
    //them tk
    public void insertTaiKhoan(TaiKhoan tk, String MSNV){
        TaiKhoan tkinsert = new TaiKhoan(tk.getTaikhoan(), Encode.encode_url_v3(tk.getMatkhau(), tk.getTaikhoan()), tk.getQuyen());
        tkinsert.setMSNV(MSNV);
        tkinsert.setIsdelete(false);
        NhanVien nv = this.nvc.getWithMSNV(MSNV);
        nv.getNguoidung().add(tkinsert);
        conn.getConn().store(tkinsert);
        conn.getConn().store(nv);   
    }
    //show tk
    public void showTimkiem(JTable tbdsnv, ObjectSet<TaiKhoan> tks){
            DefaultTableModel model = (DefaultTableModel) tbdsnv.getModel();
            int stt =0;
            for(TaiKhoan tk : tks){
                Object[]  row = {++stt, tk.getTaikhoan(), nvc.getWithMSNV(tk.getMSNV()).getHoten()};
                model.addRow(row);
            }    
            
    }
    //show khi xem nhân vien
        public void showAll_taikhoan_cuanhanvien(JTable tbdsnv,Set<TaiKhoan> tks){
        DefaultTableModel model = (DefaultTableModel) tbdsnv.getModel();
        
        if(tks.size() !=0){
            for(TaiKhoan tk : tks){
                if(tk.getIsdelete() == false){
                    Object[]  row = {tk.getTaikhoan()};
                    model.addRow(row);
                }
            }
        }
        
    }
    //dele tai khong
    public void deleteTaiKhoan(TaiKhoan tk, String MSNV){
        ObjectContainer db = conn.getConn();
        TaiKhoan tkdelete = this.getWithTenTaiKhoan(tk.getTaikhoan());
        NhanVien nv = this.nvc.getWithMSNV(MSNV);
        if(tk.getIsdelete()){
             int check = JOptionPane.showConfirmDialog(null,"Bạn có muốn xóa tài khoản này vĩnh viễn", "Warning", JOptionPane.YES_NO_OPTION);
             if(check==JOptionPane.YES_OPTION){
                 db.delete(tkdelete);
                 
                 
             }
        }
        else{
            tkdelete.setIsdelete(true);
            nv.getNguoidung().remove(tkdelete);
            db.store(tkdelete);
            db.store(nv);
        }
    }
    
    //update tai khoan
    public void updateTaiKhoan(TaiKhoan tk_moi,String check){
       NhanVien check_nv = nvc.getWithMSNV(tk_moi.getMSNV());
       if(check_nv.getIsDel() == true){
           JOptionPane.showMessageDialog(null, "Tài khoản này của nhân viên bị vô hiệu hóa hoặc không tồn tại nên không thể phục hòi");
       }
       else{
           TaiKhoan tk_update = this.getWithTenTaiKhoan(tk_moi.getTaikhoan());
           tk_update.setMatkhau(Encode.encode_url_v3(tk_moi.getMatkhau(), tk_moi.getTaikhoan()));
           tk_update.setQuyen(tk_moi.getQuyen());
           tk_update.setMSNV(tk_moi.getMSNV());
           tk_update.setIsdelete(tk_moi.getIsdelete());
           if(check.equals("phuchoi")){
               NhanVien nv = this.nvc.getWithMSNV(tk_moi.getMSNV());
               nv.getNguoidung().add(tk_update);
               this.conn.getConn().store(nv);
               
           }
           this.conn.getConn().store(tk_update);
           JOptionPane.showMessageDialog(null, "cập nhật tài khoản thành công");
       }
    }
    
}
